/**
 * 
 */
package simulation;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @author aclayton
 *
 */
public class Path implements Iterator<Configuration>, Iterable<Configuration>{
	private ArrayList<Configuration> configs;
	private int currentConfig;
	
	public Path(){
		configs = new ArrayList<Configuration>();
		currentConfig = 0;
	}
	
	public Path(ArrayList<Configuration> path){
		configs = path;
		currentConfig = 0;
	}
	
	public ArrayList<Configuration> getPathList(){
		return configs;
	}
	
	public Configuration atStep(int step){
		return configs.get(step);
	}
	
	public int getLength(){
		return configs.size();
	}
	
	public void addtoend(Configuration config){
		configs.add(config);
	}

	@Override
	public boolean hasNext() {
		if(currentConfig < configs.size())
			return true;
		return false;
	}

	@Override
	public Configuration next() {
		System.out.println(currentConfig);
		currentConfig++;
		return configs.get(currentConfig-1);
	}

	@Override
	public void remove() {
		configs.remove(currentConfig);
		currentConfig--;
	}
	
	public void reset(){
		currentConfig = 0;
	}
	
	public String toString(){
		String s = "Path_start";
		for(Configuration c : configs){
			s = s + "\n" + c.toString();	
		}
		s = s + "\nPath_end";
		return s;
	}
	
	public static Path createFromFile(Simulation s, String filename){
		Scanner sc = null;
		try {
			sc = new Scanner(new File(filename));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		
		
		String temp, config;
		temp = sc.next();
		Path p = new Path();
		boolean notdone;
		while(sc.hasNextLine()){
			temp = sc.next();
			if(temp.contains("Path_end"))
				break;
			config = temp;
			temp = sc.next();
			notdone = true;
			config = config  + '\n' + temp;
			while(notdone && sc.hasNextLine()){
				temp = sc.nextLine();
				if(temp == "")
					continue;
				if(temp.contains("Robot")|| temp.contains("Configuration_End"))
					config = config  + '\n' + temp;
				if(temp.contains("Configuration_End"))//temp.charAt(0) == 'C' && temp.charAt(1) == 'o')
					notdone = false;
			}
			
			p.addtoend(Configuration.CreateFromString(s, config));
			
			
		}
		
		return p;
	}

	@Override
	public Iterator<Configuration> iterator() {
		return this;
	}
}
